layui.config({
    base:'/static/plug-in/other/'
}).extend({
    treetable:'treetable'
}).use(['table','treetable','layer'],function () {
    var $=layui.jquery,table=layui.table,treeTable=layui.treetable,layer=layui.layer;
    var m = $('meta[name=\'_csrf\']').attr('content')
    var n = $('meta[name=\'_csrf_header\']').attr('content')
    // 渲染表格
    var renderTable = function () {
        treeTable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'parentid',
            treeDefaultClose: true,
            treeLinkage: false,
            elem: '#organization-table',
            url: '/uc/organization/ajax/tree',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'name', title: '组织名称',minWidth:200},
                {templet: '#oper-col', title: '操作按钮'}
            ]]
        });
    };
    renderTable();
    /**
     * 绑定父节点添加事件
     */
    $(".btn-add-root").on("click",function () {
        layer.open({
            type: 2,
            title: '添加节点',
            shadeClose: true,
            shade: 0.2,
            maxmin: true, //开启最大化最小化按钮
            area: ['90%', '90%'],
            content: '/uc/organization/add/entity/root',
        })
    });

    /**
     * 监听工具条事件
     */
    table.on('tool(organization-table)',function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'del') {
            deleteObj(data.id,data.name);
        } else if (layEvent === 'edit') {
            updateObj(data.id);
        }else if(layEvent ==='open'){
            $(".layui-custom-iframe").attr("src","/uc/organization/open/entity?id="+data.id);
        }
    });
    function updateObj (ObjId) {
        layer.open({
          type:2,
          title:'修改',
          shadeClose: true,
          shade: 0.2,
          maxmin: true, //开启最大化最小化按钮
          area: ['90%', '90%'],
          content: '/uc/organization/open/update?id='+ObjId,
        });
    }
    function deleteObj(ObjId,ObjName) {
        layer.confirm('您确认要删除'+ObjName+'节点吗？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                url:'/uc/organization/ajax/delete?id='+ObjId,
                dataType: 'json',
                async: false,
                beforeSend: function (xhr) {xhr.setRequestHeader(n, m)},
                contentType: 'application/json;charset=utf-8',
                success:function (data) {
                    if (data.state == 200) {
                        window.location.reload(true)
                        layer.msg('操作成功！', {time: 3000, icon: 4})
                    }
                },
                error:function () {
                    console.log("连接异常！")
                }
            });
        }, function(){
            layer.msg('取消成功！');
        });
    }
});
    